<?php
class searchEngine {
    function get_page($currentPage) {
        $this->pageid = $currentPage;
        return $this->pageid;
    }

    function get_boxValues() {
        if($this->pageid == "profile" && $_SESSION['username'] == true) {
            $this->boxValue = "Search users";
        }
        else {
            $this->boxValue = "Search newsitems";
        }
        return $this->boxValue;
    }

    function get_searchType() {
        switch($this->pageid) {
            default;
                $this->searchType = "new";
                break;

            case "7"; //profile
                if($_SESSION['username'] == true) {
                    $this->searchType = "mem";
                }
                break;
        }
        return $this->searchType;
    }

    function searchQuery($keyword_array, $type) {
        $keyword_typed = explode(' ',$keyword_array);
        $k=0;
        foreach($keyword_typed as $keyword) {
            $k++;
            $queryPiece .= "(title LIKE '%$keyword%' OR category LIKE '%$keyword%' OR tags LIKE '%$keyword%' OR summary LIKE '%$keyword%' OR body LIKE '%$keyword%') AND";
        }

        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($_SERVER["REQUEST_METHOD"] == "POST") {
            if(!empty($keyword)) {
                if($type == "new") {
                    $query = $connect->query("
                    SELECT
                        SUBSTRING(summary, 1, 250 )AS shortSummary,
                        id,
                        category,
                        tags,
                        title,
                        thumb,
                        summary,
                        body
                    FROM
                        ".$prefix."posts
                    WHERE
                        $queryPiece
                        (draft = '0')
                    ORDER BY
                        time DESC");
                }
                elseif($type == "mem") {
                    $query = $connect->query("
                    SELECT
                        *
                    FROM
                        ".$prefix."users
                    WHERE
                        username LIKE '%$keyword%' OR name LIKE '%$keyword%' OR surname LIKE '%$keyword%' OR profession LIKE '%$keyword%' OR os LIKE '%$keyword%'
                    ORDER BY
                        joined DESC");
                }
                $numrows = $query->num_rows;
                $i = 0;

                while ($row = $query->fetch_object()) {
                    if($type == "new") {
                        $commentQuery = $connect->query("SELECT id, postid FROM ".$prefix."comments WHERE page = 'news' AND postid = '".$row->id."'");
                        $commentCount = $commentQuery->num_rows;
                    }

                    $time = date('m-d',strtotime($row->time));

                    $i++;

                    if($i % 2) {
                        $class = 'odd';
                    }
                    else {
                        $class = 'even';
                    }

                    $this->searchResult[] = array(
                        "class" => $class,
                        "id" => $row->id,
                        "thumb" => $row->thumb,
                        "avatar" => $row->avatar,
                        "title" => $row->title,
                        "shortSummary" => $row->shortSummary,
                        "summary" => $row->summary,
                        "username" => $row->username,
                        "profession" => $row->profession,
                        "os" => $row->os,
                        "time" => $time,
                        "joined" => $row->joined,
                        "count" => $commentCount,
                        "resultCount" => $numrows
                    );
                }
                return $this->searchResult;
                $commentQuery->close();
                $query->close();
                $connect->close();
            }
            else {
                return false;
            }
        }
    }
}
?>